<?php


namespace App\Import;


use App\Model\Category;
use App\Model\Goods;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class GoodsImport implements ToCollection
{

    public function collection(Collection $collection)
    {
        $goods_insert = [];
        foreach ($collection->toArray() as $k => $row) {
            if ($k == 0)
                continue;
            // 查询分类
            $category = Category::where('name', $row[0])->first();
            if ($category) {
                array_push($goods_insert, [
                    'category_id' => $category->id,
                    'goods_name' => $row[1],
                    'goods_price' => $row[3],
                    'thumb_url' => $row[2],
                    'detail' => $row[4],
                    'out_url' => $row[5],
                    'status' => Goods::STATUS_ONLINE,
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now(),
                ]);
            }

        }

        Goods::insert($goods_insert);
    }

}
